import { defineStore } from "pinia";
import {
  getInventoryDetail, //盘点单查询详情
  getAddGoodsList, //盘点单物资列表
  submitExamine, //提交审核
} from "@/utils/api/ApprovalManagement/APcheck/APcheck";
import { dictlist } from "@/utils/api/dict/dict"; //字典列表
import { useMemberStore } from "@/stores/modules/member";
import { useToastStore } from "@/stores/modules/toast";
//新增列表头
const addListHeader = [
  "物资名称",
  "批次",
  "有效期",
  "库位",
  "库存数量",
  "实盘数量",
  "盘点差异",
  "单位",
];

export const useAPinventoryOtherStore = defineStore(
  "APinventoryOther",
  () => {
    const memberStore = useMemberStore();
    const toastStore = useToastStore();
    const InventoryCode: any = ref(""); //盘点单号
    const addOrdetailOrEdit = ref("edit"); //新增、详情、编辑
    // 新增盘点单物资表头
    const addtableHeader = addListHeader;
    // 盘点类型列表
    let TypeList: any = [];
    let inventoryTypeList: any = reactive({ TypeList });
    //获取类型列表
    const getGoodsTypeList = async () => {
      const res = await dictlist({
        DictStatus: "Normal",
        DictType: "InventoryType",
      });
      const arr = res.data.map((item: any) => {
        return {
          text: item.DictName,
          value: item.DictCode,
        };
      });
      inventoryTypeList.TypeList = arr;
    };

    /******************************盘点单***********************/

    // 物资信息列表
    const addGoodsList: any = [];
    const addGoodsInfoList: any = reactive({
      addGoodsList,
      currPage: 1,
      totalPage: 0,
    });
    // 获取物资信息列表
    const getAddGoods = async (data: any) => {
      const res: any = await getAddGoodsList({
        InventoryCode: InventoryCode.value,
        searchParam: data.searchParam || "",
        pageNum: data.pageNum || 1,
        pageSize: data.pageSize || 10,
      });
      let arr = res.data.list;
      //去重
      function compareObjects(obj1: any, obj2: any) {
        // 自定义比较两个对象是否相等的逻辑
        // 返回 true 表示相等，返回 false 表示不相等
        // 这里假设对象的属性都是简单类型，没有嵌套对象
        return obj1.Uuid === obj2.Uuid;
      }

      function removeDuplicateObjects(arr: any) {
        return arr.filter((el: any, index: any, self: any) => {
          return (
            index ===
            self.findIndex((obj: any) => {
              return compareObjects(obj, el);
            })
          );
        });
      }

      if (data.searchParam) {
        addGoodsInfoList.addGoodsList = res.data.list;
      } else {
        let oldaarr = JSON.parse(JSON.stringify(addGoodsInfoList.addGoodsList));
        let newarr = [...oldaarr, ...arr];
        addGoodsInfoList.addGoodsList = removeDuplicateObjects(newarr);
      }
      addGoodsInfoList.currPage = res.data.currPage;
      addGoodsInfoList.totalPage = res.data.totalPage;
      console.log("新增物资信息列表", addGoodsInfoList.addGoodsList);
    };
    /*******************************盘点单详情************************/
    const formData: any = reactive({});
    // 盘点单详情
    const inventoryOderDetail: any = reactive({});
    // 获取盘点单详情
    const getInventoryDetailData = async (data: any) => {
      const res: any = await getInventoryDetail({
        InventoryCode: data.InventoryCode,
      });
      Object.assign(inventoryOderDetail, res.data);
      const { ReplyStatus, ReplyMsg } = inventoryOderDetail;
      Object.assign(formData, {
        ReplyStatus,
        ReplyMsg,
      });
      //默认审核通过
      formData.ReplyStatus = "CheckPass";
      formData.ReplyMsg = "审核通过";
    };
    // 提交审核
    const submitAudit = async (data: any) => {
      await submitExamine({
        InventoryCode: InventoryCode.value,
        ReplyStatus: formData.ReplyStatus,
        ReplyMsg: formData.ReplyMsg,
        Replyer: memberStore.user_id,
        GodownCode: memberStore.GodownCode,
      });
      toastStore.show = true;
      toastStore.title = "审核完成";
      toastStore.type = "success";
    };
    return {
      formData,
      addtableHeader,
      addGoodsInfoList,
      getAddGoods,
      InventoryCode,
      addOrdetailOrEdit,
      getInventoryDetailData,
      inventoryOderDetail,
      inventoryTypeList,
      getGoodsTypeList,
      submitAudit,
    };
  },
  /**********************************TODO: 持久化***********************/
  {
    // 配置持久化
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
        getItem(key) {
          return uni.getStorageSync(key);
        },
      },
      paths: ["addtableHeader"],
    },
  }
);
